<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserCoinLog extends Model
{
    protected $table = 'user_coin_log';

    const TYPE_XYB = 1; //小妖币
    const TYPE_JB = 2;	//金币

    /**
     * [log description]
     * @Author   Jason
     * @DateTime 2017-01-17T19:14:57+0800
     * @param    [type]                   $bussiness_id [ 业务id ]
     * @param    [type]                   $user_id      [ 用户id ]
     * @param    [type]                   $type         [ 金币类型 ]
     * @param    [type]                   $coin         [ 金币值 ]
     * @param    [type]                   $desc         [ 日志描述 ]
     * @return   [type]                                 [description]
     */
    public static function log( $bussiness_id, $user_id, $type, $coin, $desc, $key )
    {
		$self               = new self;
		$self->user_id      = $user_id;
		$self->bussiness_id = $bussiness_id;
		$self->type         = $type;
		$self->coin         = $coin;
		$self->desc         = $desc;
        $self->key          = $key;
    	$self->save();
    }

    public static function totalXyb( $user_id )
    {
        $coin = self::where('user_id', $user_id)->where('type', 1)->sum('coin');
        if ($coin < 0) $coin = 0;
        return $coin;
    }

    public static function totalJb( $user_id )
    {
        $coin = self::where('user_id', $user_id)->where('type', 2)->sum('coin');
        if ($coin < 0) $coin = 0;
        return $coin;
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}
